package com.aibaixun.system.service.impl;

import com.aibaixun.common.core.domain.R;
import com.aibaixun.common.utils.BeanCopyUtils;
import com.aibaixun.common.utils.StringUtils;
import com.aibaixun.system.domain.TpLayerCompose;
import com.aibaixun.system.domain.dto.TpLayerComposePageDto;
import com.aibaixun.system.domain.dto.TpLayerPageDto;
import com.aibaixun.system.service.TpLayerComposeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aibaixun.system.domain.TpLayer;
import com.aibaixun.system.service.TpLayerService;
import com.aibaixun.system.mapper.TpLayerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author zhangyh
* @description 针对表【tp_layer(拓扑图层信息)】的数据库操作Service实现
* @createDate 2024-06-14 17:30:23
*/
@Service
public class TpLayerServiceImpl extends ServiceImpl<TpLayerMapper, TpLayer>
    implements TpLayerService{

    @Autowired
    private TpLayerComposeService tpLayerComposeService;

    @Override
    public R saveLayer(TpLayer dto) {
        if(StringUtils.isBlank(dto.getLayerName())){
            return R.fail("名称不能为空");
        }
        Long count = this.baseMapper.selectCount(new LambdaQueryWrapper<TpLayer>().eq(TpLayer::getLayerName,dto.getLayerName()));
        if(count > 0){
            return R.fail("名称已存在");
        }
        this.baseMapper.insert(dto);
        return R.ok();
    }

    @Override
    public R updateLayer(TpLayer dto) {
        if(dto.getLayerId() == null){
            return R.fail("id不能为空");
        }
        TpLayer old = this.baseMapper.selectById(dto.getLayerId());
        if(old == null){
            return R.fail("图层信息异常");
        }
        if(dto.getLayerName() != null && !StringUtils.equals(dto.getLayerName(),old.getLayerName())){
            Long count = this.baseMapper.selectCount(new LambdaQueryWrapper<TpLayer>().eq(TpLayer::getLayerName,dto.getLayerName()));
            if(count > 0){
                return R.fail("名称已存在");
            }
        }
        BeanCopyUtils.copy(dto,old);
        this.baseMapper.updateById(old);
        return R.ok();
    }

    @Override
    public R deleteLayer(Long layerId) {
        this.baseMapper.deleteById(layerId);
        return R.ok();
    }

    @Override
    public R<Page<TpLayer>> getLayerDataPage(Page page, TpLayerPageDto pageDto) {
        TpLayer tpLayer = new TpLayer();
        BeanCopyUtils.copy(pageDto,tpLayer);
        Page<TpLayer> resultPage = this.page(page,new LambdaQueryWrapper<>(tpLayer));
        return R.ok(resultPage);
    }

    @Override
    public R saveLayerCompose(TpLayerCompose dto) {
        dto.setIsShow(0);
        tpLayerComposeService.getBaseMapper().insert(dto);
        return R.ok();
    }

    @Override
    @Transactional
    public R updateLayerCompose(TpLayerCompose dto) {
        if(dto.getComposeId() == null){
            return R.fail("id不能为空");
        }
        TpLayerCompose old = tpLayerComposeService.getById(dto.getComposeId());
        if(old == null){
            return R.fail("组合信息异常");
        }

        if(dto.getIsShow().intValue() == 1){
            tpLayerComposeService.update(
                new LambdaUpdateWrapper<TpLayerCompose>()
                .set(TpLayerCompose::getIsShow,0)
            );
        }

        BeanCopyUtils.copy(dto,old);
        tpLayerComposeService.getBaseMapper().updateById(old);
        return R.ok();
    }

    @Override
    public R deleteLayerCompose(Long layerId) {
        tpLayerComposeService.getBaseMapper().deleteById(layerId);
        return R.ok();
    }

    @Override
    public R<TpLayerCompose> getLayerComposeDetail(Long composeId) {
        TpLayerCompose old = tpLayerComposeService.getById(composeId);
        if(old == null){
            return R.fail("组合信息异常");
        }
        if(StringUtils.isNotBlank(old.getLayerIds())){
            old.setLayerList(
                this.baseMapper.selectList(new LambdaQueryWrapper<TpLayer>().in(TpLayer::getLayerId,old.getLayerIds().split(",")))
            );
        }

        return R.ok(old);
    }

    @Override
    public R<Page<TpLayerCompose>> getLayerComposeDataPage(Page page, TpLayerComposePageDto pageDto) {
        TpLayerCompose old = new TpLayerCompose();
        BeanCopyUtils.copy(pageDto,old);
        Page<TpLayerCompose> resultPage = tpLayerComposeService.page(page,new LambdaQueryWrapper<>(old));
        for (TpLayerCompose record : resultPage.getRecords()) {
            record.setLayerList(
                this.baseMapper.selectList(new LambdaQueryWrapper<TpLayer>().in(TpLayer::getLayerId,record.getLayerIds().split(",")))
            );
        }
        return R.ok(resultPage);
    }

    @Override
    public R getAvailableCompose() {
        TpLayerCompose old = tpLayerComposeService.getOne(
            new LambdaQueryWrapper<TpLayerCompose>()
            .eq(TpLayerCompose::getIsShow,1)
            .last("limit 1")
        );
        if(old == null){
            return R.fail("暂无可用图层配置信息");
        }
        if(StringUtils.isNotBlank(old.getLayerIds())){
            old.setLayerList(
                this.baseMapper.selectList(new LambdaQueryWrapper<TpLayer>().in(TpLayer::getLayerId,old.getLayerIds().split(",")))
            );
        }

        return R.ok(old);
    }
}




